/**     
* @Title: GroupController.java
* @Package me.springboot.mybatis.web
* @Description: TODO
* @author zengh 
* @date 2018/04/19
* @version V1.0     
*/
package me.springboot.mybatis.web;

import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Group;
import me.springboot.mybatis.service.GroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**   
* @ClassName: GroupController   
* @Description: TODO
* @author zengh
* @date 2018/04/19
*/
@RestController
@RequestMapping("/group")
@CrossOrigin
public class GroupController {

    @Resource
    private GroupService groupService;

    /**
	* @Title: add   
	* @Description: 添加
	* @param group
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/add")
    public RetResult<Object> add(Group group) {
        groupService.save(group);
        return RetResponse.makeOKRsp();
    }
    
    /**
	* @Title: add2   
	* @Description: 传入参数为JSON格式
	* @param group
	* @Reutrn RetResult<Object>
	*/
	@PostMapping("/add2")
	public RetResult<Object> add2(@RequestBody Group group) {
		groupService.save(group);
		return RetResponse.makeOKRsp();
	}

    /**
	* @Title: delete   
	* @Description: 删除
	* @param group
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) throws ResultException {
		groupService.deleteGroup(id);
        return RetResponse.makeOKRsp();
    }

	@PostMapping("/deleteByIds")
	public RetResult<Object> deleteByIds(@RequestParam String ids) throws ResultException {
		groupService.deleteGroupByIds(ids);
		return RetResponse.makeOKRsp();
	}

    /**
	* @Title: update   
	* @Description: 更新
	* @param group
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/update")
    public RetResult<Object> update(Group group) {
        groupService.update(group);
        return RetResponse.makeOKRsp();
    }
    
    /**
	* @Title: update   
	* @Description: 更新(传入参数为JSON格式)
	* @param group
	* @Reutrn RetResult<Object>
	*/
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody Group group) {
        groupService.update(group);
        return RetResponse.makeOKRsp();
    }

    /**
	* @Title: detail   
	* @Description: 根据ID查询详情
	* @param group
	* @Reutrn RetResult<Group>
	*/
    @PostMapping("/detail")
    public RetResult<Group> detail(@RequestParam Integer id) {
        Group group = groupService.findById(id);
        return RetResponse.makeOKRsp(group);
    }
    
    /**
	* @Title: detail   
	* @Description: 传入参数为JSON格式
	* @param reqMap
	* @Reutrn RetResult<Group>
	*/
	@PostMapping("/detail2")
	public RetResult<Group> detail(@RequestBody Map<String, Object> reqMap) {
		Integer id = Integer.valueOf(reqMap.get("id").toString());
		Group group = groupService.findById(id);
        return RetResponse.makeOKRsp(group);
	}

    /**
	* @Title: list   
	* @Description: 分页查询
	* @param page 页码
	* @param size 每页条数
	* @Reutrn RetResult<PageInfo<Group>>
	*/
    @PostMapping("/list")
    public RetResult<PageInfo<Group>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Group> list = groupService.findAll();
        PageInfo<Group> pageInfo = new PageInfo<Group>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

	@GetMapping("/getAll")
	@CrossOrigin
	public RetResult<List<Group>> getAll() {
		List<Group> list = groupService.findAll();
		return RetResponse.makeOKRsp(list);
	}


}